package com.link.base.base.stocksellstorage.controller;

import com.link.core.cllog.LCLogger;
import com.link.base.base.stocksellstorage.model.Storage;
import com.link.base.base.stocksellstorage.model.StorageProd;
import com.link.base.base.stocksellstorage.model.StorageProdBalance;
import com.link.base.base.stocksellstorage.service.StorageProdBalanceService;
import com.link.base.base.stocksellstorage.service.StorageProdService;
import com.link.base.base.stocksellstorage.service.StorageService;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author unknown
 * @date unknown
 * @comments 仓库
 */
@Controller
@RequestMapping(value = "/link/stockSellStorage")
public class StorageController extends BasicController<Storage> {
    @Resource
    private StorageService storageService;
    @Resource
    private StorageProdService prodService;
    @Resource
    private StorageProdBalanceService prodBalanceService;

    @RequestMapping(value = "/queryByExamplePage", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Map<String, Object> queryByExamplePage(Storage storage, HttpSession session, HttpServletRequest request,
                                                  HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();

        // 当前记录
        int currentCount = storage.getCurrentCount();
        // 总记录数
        int count = 0;

        try {
            if (currentCount == 0) {
                List list = storageService.queryAll(storage);
                count = list != null ? list.size() : 0;
            }
            storage.setStartRow(currentCount + 1);
            storage.setEndRow(currentCount + storage.getPage());

            List<Storage> list = storageService.queryByExamplePage(storage);
            StorageProd storageProd = null;
            for (Storage s : list) {
                // 根据库存Id查询产品列表
                storageProd = new StorageProd();
                storageProd.setInvlocId(s.getId());
                List<StorageProd> storageProdList = prodService.queryAll(storageProd);
                s.setProdList(storageProdList);
            }

            result.put("count", count);
            result.put("result", list);
            result.put("success", true);

        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
            LCLogger.withException(e);
        }
        return result;
    }

    /**
     * 根据Id查询库存详情
     *
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "queryProdListByStorageId", method = RequestMethod.GET, produces = "application/json")
    public Map<String, Object> queryStorageById(HttpSession session, HttpServletRequest request,
                                                HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        String id = request.getParameter("id");

        try {
            // 查询库存信息

            StorageProd storageProd = new StorageProd();
            // 根据库存Id查询产品列表
            storageProd.setInvlocId(Long.parseLong(id));
            List<StorageProd> storageProdList = prodService.queryAll(storageProd);

            StorageProdBalance prodBalance = null;
            List<StorageProdBalance> balanceList = null;
            for (StorageProd sp : storageProdList) {
                prodBalance = new StorageProdBalance();
                prodBalance.setStockId(sp.getId());
                balanceList = prodBalanceService.queryAll(prodBalance);
                sp.setBalanceList(balanceList);
            }

            result.put("result", storageProdList);
            result.put("success", true);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
            LCLogger.withException(e);
        }
        return result;
    }

    @Override
    public BasicService<Storage> getBasicService() throws Exception {
        return storageService;
    }
}
